﻿
/****************************** 
 * 
 *  Create By: Arthur.li
 *  
 *  Create At: 2009年4月24日 10:41:12
 *  
 *  Description: 移动设备管理
 *  
 *  Remark: 
 *  
 * ****************************/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Jumpton.Persistent;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;

public partial class System_Mobile_MobileManager : PageBase
{
    // 当前对该设备的操作提示
    String _controlInfo = " -- {0}";

    protected void Page_Load(object sender, EventArgs e)
    {
     
        if (!IsPostBack)
        {
            CreateMobile();
            BindData();
        }
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (txtIMEI.Value.Equals(""))
        {
            ShowMessage(Resources.Mobile.HaveNotCompleteMobileInfo);
            return;
        }

        MobileDeviceInfo mobile = null;

        if (txtSelect.Value.Equals("")) // 当前为创建
        {
            // 判断输入的IMEI是否已经存在
            mobile = new MobileDeviceInfoManager().LoadByCode(txtIMEI.Value);

            if (mobile != null) // 已经存在
            {
                ShowMessage(Resources.Mobile.IMEIExists);
                return;
            }

            // 创建一个
            mobile = new MobileDeviceInfo();
        }
        else
        {
            // 获得当前修改的移动设备信息
            mobile = new MobileDeviceInfoManager().LoadByCode(txtSelect.Value);
        }

        mobile.IMEICode = txtIMEI.Value;
        mobile.ACTIVE = rbtnIsActive.Items[0].Selected;
        mobile.Model = txtModel.Value;
        mobile.BrandName = txtBrand.Value;
        mobile.LASTUP_DATER = MyStateManager.Name;
        mobile.Save();

        if (mobile.ID > 0)
        {
            // 提示修改成功
            ShowMessage(Resources.Mobile.ModifyDeviceSuccess);
        }
        else
        {
            // 提示创建成功
            ShowMessage(Resources.Mobile.CreateDeviceSuccess);
        }

        // 没有选择"继续创建", 或者当前为修改操作
        if (!chkMore.Visible || !chkMore.Checked || !txtSelect.Value.Equals(""))
        {
            chkMore.Visible = false;
            PreCreate(false);
            litInfo.Text = "";
        }
        Reset();
        BindData();
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Reset();
    }

    /// <summary>
    /// 重置移动设备文本框信息
    /// </summary>
    private void Reset()
    {
        txtIMEI.Value = txtModel.Value = txtBrand.Value = "";
        rbtnIsActive.Items[0].Selected = true;

        //txtSelect.Value = "";
    }

    /// <summary>
    /// 向设备列表中绑定数据
    /// </summary>
    protected void BindData()
    {
        ObjectTable table = new MobileDeviceInfoManager().QuickSearch(txtSearch.Value);
        BindData(table);

    }

    /// <summary>
    /// 绑定数据到GridView
    /// </summary>
    /// <param name="source"></param>
    private void BindData(object source)
    {
        gvResult.DataSource = source;
        gvResult.DataBind();
       
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        CreateMobile();
    }

    /// <summary>
    /// 创建移动设备信息
    /// </summary>
    private void CreateMobile()
    {
        // 清空隐藏控件内容
        txtSelect.Value = "";
        Reset();
        chkMore.Visible = true;
        PreCreate(true);
        txtIMEI.Focus();
        // 提示正在创建
        litInfo.Text = String.Format(_controlInfo, Resources.SiteMap.CreateText);
    }

    /// <summary>
    /// 准备创建移动设备
    /// </summary>
    /// <param name="can"></param>
    private void PreCreate(Boolean can)
    {
        btnReset.Enabled = can;
        btnOk.Enabled = can;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void gvResult_SelectedIndexChanged(object sender, EventArgs e)
    {
        // 提示正在修改
        litInfo.Text = String.Format(_controlInfo, Resources.SiteMap.ModifyText);

        MobileDeviceInfo deviceInfo = new MobileDeviceInfoManager().LoadByCode(gvResult.SelectedValue.ToString());

        if (deviceInfo == null)
        {
            return;
        }

        // 隐藏控件记录Mobile的IMEI信息
        txtSelect.Value = deviceInfo.IMEICode;
        txtBrand.Value = deviceInfo.BrandName;
        txtIMEI.Value = deviceInfo.IMEICode;
        txtModel.Value = deviceInfo.Model;
        txtUpdateBy.Value = deviceInfo.LASTUP_DATER;
        txtUpdateTime.Value = deviceInfo.LAST_UPDATE_TIME.ToString();
        rbtnIsActive.Items[0].Selected = deviceInfo.ACTIVE;
        chkMore.Visible = false;
        PreCreate(true);
    }
    protected void PageIndexChanged(object sender, GridViewPageEventArgs e)
    {
        gvResult.PageIndex = e.NewPageIndex;
        BindData();
    }
}
